package com.springboot.mybatis.base.controller;

import com.springboot.mybatis.base.entity.PageInfo;
import com.springboot.mybatis.base.response.GenericResponse;
import com.springboot.mybatis.base.service.BaseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.concurrent.Callable;

public abstract class BaseController<T, ID extends Serializable> {
    public BaseService service;

    public BaseController() {
    }

    public abstract BaseService getService();

    @GetMapping({""})
    @ApiOperation(value = "findAll:查询全部数据", notes = "findAll:查询全部数据")
    public Callable<GenericResponse<PageInfo>> findAll() {
        return () -> {
            return GenericResponse.success(this.getService().findAllWithResult((Object)null));
        };
    }

    @PostMapping({"/page"})
    @ApiOperation(value = "findAll:分页查询全部数据", notes = "findAll:分页查询全部数据")
    public Callable<GenericResponse<PageInfo>> findAll(@RequestBody(required = false) T record) {
        return () -> {
            return record == null ? GenericResponse.success(this.getService().findAllWithPageInfo(record)) : GenericResponse.success(this.getService().findPageWithPageInfo(record));
        };
    }

    @PostMapping({"/page/like"})
    @ApiOperation(value = "findLike:分页模糊查询全部数据", notes = "findLike:分页模糊查询全部数据")
    public Callable<GenericResponse<PageInfo>> findLike(@RequestBody(required = false) T record) {
        return () -> {
            return record == null ? GenericResponse.success(this.getService().findAllWithPageInfo(record)) : GenericResponse.success(this.getService().findPageWithPageInfoLike(record));
        };
    }

    @GetMapping({"/{id}"})
    @ApiOperation(value = "findByID:根据id查询单个数据", notes = "findByID:根据id查询单个数据")
    public Callable<GenericResponse<T>> findByID(@PathVariable("id") ID id) {
        return () -> {
            return GenericResponse.success(this.getService().selectById(id));
        };
    }

    @PostMapping({""})
    @ApiOperation(value = "add:新增数据", notes = "add:新增数据")
    public Callable<GenericResponse<T>> add(@RequestBody T record) {
        return () -> {
            return GenericResponse.success(this.getService().insertSelective(record));
        };
    }

    @PutMapping({"/{id}"})
    @ApiOperation(value = "edit:根据id修改数据-全部修改", notes = "edit:根据id修改数据-全部修改")
    public Callable<GenericResponse<T>> edit(@RequestBody T record, @PathVariable(value = "id",required = false) ID id) {
        return () -> {
            return GenericResponse.success(this.getService().updateByPrimaryKey(record));
        };
    }

    @PatchMapping({"/{id}"})
    @ApiOperation(value = "editSelective:根据id修改数据-有值则修改", notes = "editSelective:根据id修改数据-有值则修改")
    public Callable<GenericResponse<T>> editSelective(@RequestBody T record, @PathVariable(value = "id",required = false) ID id) {
        return () -> {
            return GenericResponse.success(this.getService().updateByPrimaryKeySelective(record));
        };
    }

    @PostMapping({"/edit/{id}"})
    @ApiOperation(value = "editPost:根据id修改数据-有值则修改", notes = "editPost;根据id修改数据-有值则修改")
    public Callable<GenericResponse<T>> editPost(@RequestBody T record, @PathVariable(value = "id",required = false) ID id) {
        return () -> {
            return GenericResponse.success(this.getService().updateByPrimaryKeySelective(record));
        };
    }

    @DeleteMapping({"/{id}"})
    @ApiOperation(value = "delete:根据id删除数据", notes = "delete:根据id删除数据")
    public Callable<GenericResponse<T>> delete(@PathVariable("id") ID id) {
        return () -> {
            return GenericResponse.success(this.getService().deleteByPrimaryKey(id));
        };
    }

    @PostMapping({"/exist"})
    public Callable<GenericResponse<Boolean>> exist(@RequestBody T record) {
        return () -> {
            return GenericResponse.success(this.getService().exist(record));
        };
    }

    @GetMapping({"/logical/hide/{id}"})
    public Callable<GenericResponse<T>> logicalHide(@PathVariable("id") ID id) {
        return () -> {
            return GenericResponse.success(this.getService().logicalHide(id));
        };
    }

    @GetMapping({"/logical/show/{id}"})
    public Callable<GenericResponse<T>> logicalShow(@PathVariable("id") ID id) {
        return () -> {
            return GenericResponse.success(this.getService().logicalShow(id));
        };
    }

    @DeleteMapping({"/logical/delete/{id}"})
    public Callable<GenericResponse<T>> logicalDelete(@PathVariable("id") ID id) {
        return () -> {
            return GenericResponse.success(this.getService().logicalDelete(id));
        };
    }
}